
package com.jf.cloud.multishop.controller.admin;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.multishop.dto.ShopWalletLogSearchDTO;
import com.jf.cloud.multishop.dto.ShopWithdrawCashDTO;
import com.jf.cloud.multishop.service.ShopWithdrawCashService;
import com.jf.cloud.multishop.vo.ShopWalletLogExcelVO;
import com.jf.cloud.multishop.vo.ShopWithdrawCashLogExcelVO;
import com.jf.cloud.multishop.vo.ShopWithdrawCashVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 商家提现申请信息
 *
 * @author zz
 * @Date 2021/6/11 9:23
 */
@RestController("adminShopWithdrawCashController")
@RequestMapping("/mp/shop_withdraw_cash")
@Tag(name = "admin-商家提现申请信息")
public class ShopWithdrawCashController {

    @Autowired
    private ShopWithdrawCashService shopWithdrawCashService;

    @GetMapping("/page")
    @Operation(summary = "分页获取商家提现记录信息" , description = "分页获取商家提现记录信息")
    public ServerResponseEntity<PageVO<ShopWithdrawCashVO>> page(PageDTO pageDTO, @ParameterObject ShopWithdrawCashDTO shopWithdrawCashDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            shopWithdrawCashDTO.setShopId(shopId);
        }
        PageVO<ShopWithdrawCashVO> pageVO = shopWithdrawCashService.pageByParam(pageDTO, shopWithdrawCashDTO);
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/soldExcel")
    @Operation(summary = "导出商家提现表", description = "导出商家提现表")
    public ServerResponseEntity<Void> soldExcel(HttpServletResponse response, @ParameterObject ShopWithdrawCashDTO shopWithdrawCashDTO) {
        List<ShopWithdrawCashLogExcelVO> list = shopWithdrawCashService.listShopWithdrawCashLogExcel(shopWithdrawCashDTO);
        for (int i = 0; i < list.size(); i++) {
            ShopWithdrawCashLogExcelVO su=list.get(i);
            //状态(0:未审核、1:已通过 -1:未通过 2:发放成功 3:发放失败)
            if(su.getStatus()==0){
                su.setStatusStr("未审核");
            }else if(su.getStatus()==1){
                su.setStatusStr("已通过");
            }else if(su.getStatus()==-1){
                su.setStatusStr("未通过");
            }else if(su.getStatus()==2){
                su.setStatusStr("发放成功");
            }else if(su.getStatus()==3){
                su.setStatusStr("发放失败");
            }

        }
        ExcelUtil.soleExcel(response, list, ShopWithdrawCashLogExcelVO.EXCEL_NAME, ShopWithdrawCashLogExcelVO.MERGE_ROW_INDEX, ShopWithdrawCashLogExcelVO.MERGE_COLUMN_INDEX, ShopWithdrawCashLogExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/info")
    @Operation(summary = "根据提现记录id查看提现记录" , description = "根据提现记录id查看提现记录")
    public ServerResponseEntity<ShopWithdrawCashVO> getById(@ParameterObject @RequestParam("cashId") Long cashId) {
        ShopWithdrawCashVO shopWithdrawCashVO = shopWithdrawCashService.getDetailByCashId(cashId);
        return ServerResponseEntity.success(shopWithdrawCashVO);
    }
}
